package com.codeye.spider.siteName;

import java.io.IOException;
import java.net.ConnectException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Actions_sn {
	private static boolean accessAble(String htmlStr) {
		if (null != htmlStr) {
			if (htmlStr.indexOf("Local function for getting a cookie value") != -1) {
				return false;
			} else if (htmlStr.indexOf("114导航") != -1) {
				return false;
			} else if (htmlStr.indexOf("404 Not Found") != -1) {
				return false;
			} else if (htmlStr.indexOf("403 Forbidden") != -1) {
				return false;
			} else if (htmlStr.indexOf("500 Internal Server Error") != -1) {
				return false;
			} else if (htmlStr.indexOf("400 Bad Request") != -1) {
				return false;
			} else if (htmlStr.indexOf("链接不存在") != -1) {
				return false;
			}
		} else {
			return false;
		}
		return true;
	}
	
	public static Map<String, String> grabWebName(String url, int SpiderNum, int restSize) {
		String fullUlr = url;
		String htmlStr = null;
		boolean accessAbleFlag = true;
		
		if(!url.trim().equals("")){
			if(url.indexOf("http") == -1){
				fullUlr = "http://" + url;
			}
			htmlStr = getHtmlContent(fullUlr, "UTF-8");	// https://      http://www.
			if (!accessAble(htmlStr)) {
				if(url.indexOf("http://www.") == -1){
					fullUlr = "http://www." + url;
					htmlStr = getHtmlContent(fullUlr, "UTF-8");
					if (!accessAble(htmlStr)) {
						if(url.indexOf("https://") == -1){
							fullUlr = "https://" + url;	
							htmlStr = getHtmlContent(fullUlr, "UTF-8");
							if (!accessAble(htmlStr)) {
								accessAbleFlag = false;
							}
						}else{accessAbleFlag = false;}
					}
				}else{accessAbleFlag = false;}
			}
		}
		
		String elemText = null;
		Map<String, String> unkv = new HashMap<String, String>();
		if(null != htmlStr && accessAbleFlag){
			if(htmlStr.indexOf("�") != -1){
				htmlStr = getHtmlContent(fullUlr, "gb2312");
				if(null != htmlStr && htmlStr.indexOf("�") != -1){
					htmlStr = getHtmlContent(fullUlr, "gbk");
				}
			}
			
			if(null == htmlStr){
				unkv.put(url, "unknow");
				return unkv;
			}
			Document doc = Jsoup.parse(htmlStr);
			
			
			// 检查Title
			Elements elemst = doc.select("title");
			if(elemst.size() != 0){
				for(Element elem : elemst){
					elemText = elem.text();
				}
				unkv.put(url, elemText);
				return unkv;
			}
			// 检查 Meta
			Elements elemsm = doc.getElementsByAttribute("keywords");
			if(elemsm.size() != 0){
				for(Element elem : elemst){
					String c = elem.attr("content");
					int endIndex = c.indexOf("c");
					if(endIndex != -1){
						elemText = c.substring(0, endIndex);
					}
				}
				unkv.put(url, elemText);
				return unkv;
			}
		}
		unkv.put(url, "unknow");
		return unkv;
	}

	public static void main(String[] args) throws SQLException {
		String url = "www.ss0730.com";
		Map<String, String> rs = grabWebName(url, 0, 0);
		System.out.println(rs);
	}
	
	private static String getHtmlContent(String url, String charset) {
		if(url.equalsIgnoreCase("http://")){
			return null;
		}
		RequestConfig requestConfig = RequestConfig.custom()
				.setSocketTimeout(30000)
				.setConnectTimeout(30000)
				.setConnectionRequestTimeout(30000).build();
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("account", ""));
		formparams.add(new BasicNameValuePair("password", ""));

		HttpGet hget = new HttpGet(url);
//		hget.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0");
		hget.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
		//
		hget.setConfig(requestConfig);

		// CloseableHttpClient httpclient = HttpClients.createDefault();
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();

		String htmlContent = null;
		CloseableHttpResponse response = null;
		try {
			response = httpClient.execute(hget);
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode == HttpStatus.SC_OK) {
				HttpEntity resEntity = response.getEntity();
				htmlContent = EntityUtils.toString(resEntity, charset);
			} else {
				System.err.println("Method failed: " + response.getStatusLine() + " " + url);
				return null;
			}
			
		} catch (ConnectException e) {
//			System.err.println("ConnectException >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + url);
		} catch (IOException e) {
//			System.err.println("IOException >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + url);
		} catch (IllegalArgumentException e) {
//			System.err.println("IllegalArgumentException >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + url);
		} catch (IllegalStateException e) {
//			System.err.println("IllegalStateException >>>>>>>>>>>>>>>Target host is null>>>>>>>>>>>>>>>" + url);
		} finally {
			try {
				if(null != response){
					response.close();
				}
				httpClient.close();
			} catch (IOException e) {
				System.err.println("IOException!        close");
			}
		}
		return htmlContent;
	}
}
